package library.and.xps.com.step.util;

import com.jude.utils.JUtils;

import io.realm.Realm;
import library.and.xps.com.step.m.model.User;

/**
 * Created by XPS on 2017/5/20.
 */

public class UserHelper {
    private static final UserHelper ourInstance = new UserHelper();
    private Realm realm;

    public static UserHelper getInstance() {
        return ourInstance;
    }

    private UserHelper() {
        realm = Realm.getDefaultInstance();
    }

    /**
     * 信息修改成功调用后 修改本地数据
     *
     * @param feild 用户信息字段
     * @param value 值
     * @return 修改是否成功
     */
    public boolean updateUser(String feild, String value) {
        JUtils.Log("updateUser--"+"feild:"+feild);
        JUtils.Log("updateUser--"+"value:"+value);
        realm.beginTransaction();
        User user = realm.where(User.class).findFirst();

        if (user == null) {
            realm.commitTransaction();
            return false;
        }

        if (feild.equals("ico")) {
            user.setIco(value);
        } else if (feild.equals("sex")) {
            user.setSex(value);
        } else if (feild.equals("weight")) {
            user.setWeight(value);
        } else if (feild.equals("stature")) {
            user.setStature(value);
        } else if (feild.equals("nickname")) {
            user.setNickname(value);
        } else if (feild.equals("goal")) {
            user.setStepperday(value);
        } else if (feild.equals("jpushid")) {
            user.setJpushId(value);
        }

        realm.commitTransaction();
        return true;
    }

    public User getUser(){
        User user;
        if (realm != null) {
            user = realm.where(User.class).findFirst();
            return user;
        }

        return null;
    }

}
